/*  JInternalFrameCriarNovoJogo.java
 *  Copyright (C) 2009  Tiago Silva Loureiro
 *  This File is part of Bier Manager.
 *  Bier Manager a is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/*  JInternalFrameCriarNovoJogo.java - Bier Manager
 *
 *  Tela para se criar um usuário iniciando o processo de criação de novo jogo.
 *  Abre o banco de dados de contas e verifica se o usuario digitado
 *  corresponde a algum usuário já cadastrado para iniciar o jogo.
 *  @author Tiago Silva Loureiro
 * *
 */
package biermanager.interfaceGrafica;

import biermanager.nucleo.ConstantesDeCaminho;
import biermanager.nucleo.Conta;
import biermanager.nucleo.DadosContas;
import biermanager.nucleo.Usuario;
import biermanager.componentes.Eventos;
import biermanager.componentes.PintorDeBackground;
import biermanager.componentes.PintorDeForeground;
import com.db4o.ObjectSet;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JOptionPane;
import org.jdesktop.swingx.painter.AbstractLayoutPainter.HorizontalAlignment;
import org.jdesktop.swingx.painter.AbstractLayoutPainter.VerticalAlignment;
import org.jdesktop.swingx.painter.CompoundPainter;
import org.jdesktop.swingx.painter.ImagePainter;

public class JInternalFrameCriarNovoJogo extends javax.swing.JInternalFrame {

    private float alpha;
    private PintorDeBackground pintor;
    private DadosContas dadosContas;
    private Conta contaTemporaria;
    private ObjectSet contas;

    /** Construtor JInternalFrameCriarNovoJogo */
    public JInternalFrameCriarNovoJogo() {
        initComponents();
        iniciarDisplay();
        iniciarBackgrounds();
        iniciarForegrounds();
    }

    private void iniciarDisplay() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((int) ((screenSize.getWidth() - this.getWidth()) / 2), 50);
        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null); //Retira a barra de título
        this.setDefaultCloseOperation(JInternalFrameCriarNovoJogo.DO_NOTHING_ON_CLOSE);
    }

    private void iniciarBackgrounds() {
        pintor = new PintorDeBackground();
        ImagePainter imp = (ImagePainter) pintor.pintorIcone(ConstantesDeCaminho.novoJogo, 100, 100);
        imp.setHorizontalAlignment(HorizontalAlignment.LEFT);
        imp.setVerticalAlignment(VerticalAlignment.CENTER);
        jxPainelEstrutural.setBackgroundPainter(new CompoundPainter(pintor.pintarDegradePreto(jxPainelEstrutural),imp));
        jxlabelBotaoContinuar.setBackgroundPainter(pintor.pintarBotaoSemFocoCapsulaPreto());
        jxlabelBotaoFechar.setBackgroundPainter(pintor.pintarCinzaTransparente(jxlabelBotaoFechar));
    }
    private void iniciarForegrounds(){
        jxlabelBotaoContinuar.setForeground(PintorDeForeground.corBotao());
        jxlabelBotaoFechar.setForeground(PintorDeForeground.corFechar());
    }

    private void setAlpha() {
        this.jxPainelEstrutural.setAlpha(alpha);
    }

    /**
     * Gerado automaticamente pelo matisse( Editor visual do NetBeans ).
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jxPainelInvisivel = new org.jdesktop.swingx.JXPanel();
        jxPainelEstrutural = new org.jdesktop.swingx.JXPanel();
        jlabelNome = new javax.swing.JLabel();
        jlabelSenha = new javax.swing.JLabel();
        jlabelConfirmarSenha = new javax.swing.JLabel();
        jtextfieldNome = new javax.swing.JTextField();
        jpasswordfieldSenha = new javax.swing.JPasswordField();
        jpasswordfieldConfirmarSenha = new javax.swing.JPasswordField();
        jlabelRegistrar = new javax.swing.JLabel();
        jlabelImagemTopo = new javax.swing.JLabel();
        jxlabelBotaoContinuar = new org.jdesktop.swingx.JXLabel();
        jxlabelBotaoFechar = new org.jdesktop.swingx.JXLabel();

        setBackground(new java.awt.Color(0, 0, 0));
        setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102))));
        setPreferredSize(new java.awt.Dimension(455, 260));
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jxPainelInvisivel.setAlpha(0f);
        jxPainelInvisivel.setOpaque(false);

        jxPainelEstrutural.setAlpha(0.f);
        jxPainelEstrutural.setInheritAlpha(false);
        jxPainelEstrutural.setOpaque(false);

        jlabelNome.setBackground(new java.awt.Color(255, 255, 255));
        jlabelNome.setFont(new java.awt.Font("Tahoma", 1, 12));
        jlabelNome.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlabelNome.setText("Nome de usuário:");
        jlabelNome.setForeground(new java.awt.Color(255, 255, 255));
        jlabelNome.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        jlabelSenha.setBackground(new java.awt.Color(255, 255, 255));
        jlabelSenha.setFont(new java.awt.Font("Tahoma", 1, 12));
        jlabelSenha.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlabelSenha.setText("Senha:");
        jlabelSenha.setForeground(new java.awt.Color(255, 255, 255));
        jlabelSenha.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        jlabelConfirmarSenha.setBackground(new java.awt.Color(255, 255, 255));
        jlabelConfirmarSenha.setFont(new java.awt.Font("Tahoma", 1, 12));
        jlabelConfirmarSenha.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlabelConfirmarSenha.setText("Confirme a senha:");
        jlabelConfirmarSenha.setForeground(new java.awt.Color(255, 255, 255));
        jlabelConfirmarSenha.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        jtextfieldNome.setFont(new java.awt.Font("Tahoma", 1, 12));
        jtextfieldNome.setOpaque(false);
        jtextfieldNome.setForeground(new java.awt.Color(255, 255, 255));

        jpasswordfieldSenha.setFont(new java.awt.Font("Tahoma", 1, 12));
        jpasswordfieldSenha.setOpaque(false);
        jpasswordfieldSenha.setForeground(new java.awt.Color(255, 255, 255));

        jpasswordfieldConfirmarSenha.setFont(new java.awt.Font("Tahoma", 1, 12));
        jpasswordfieldConfirmarSenha.setOpaque(false);
        jpasswordfieldConfirmarSenha.setForeground(new java.awt.Color(255, 255, 255));

        jlabelRegistrar.setFont(new java.awt.Font("Tahoma", 1, 16));
        jlabelRegistrar.setText("Registrar um novo usuário:");
        jlabelRegistrar.setForeground(new java.awt.Color(255, 255, 255));

        jlabelImagemTopo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/biermanager/imagens/novojogo.png"))); // NOI18N

        jxlabelBotaoContinuar.setForeground(new java.awt.Color(50, 50, 50));
        jxlabelBotaoContinuar.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jxlabelBotaoContinuar.setText("Continuar!");
        jxlabelBotaoContinuar.setFont(new java.awt.Font("Tahoma", 1, 12));
        jxlabelBotaoContinuar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jxlabelBotaoContinuarMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jxlabelBotaoContinuarMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jxlabelBotaoContinuarMouseExited(evt);
            }
        });

        jxlabelBotaoFechar.setForeground(new java.awt.Color(255, 255, 255));
        jxlabelBotaoFechar.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jxlabelBotaoFechar.setText("x");
        jxlabelBotaoFechar.setFont(new java.awt.Font("Tahoma", 1, 14));
        jxlabelBotaoFechar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jxlabelBotaoFechar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jxlabelBotaoFecharMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jxlabelBotaoFecharMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jxlabelBotaoFecharMouseExited(evt);
            }
        });

        javax.swing.GroupLayout jxPainelEstruturalLayout = new javax.swing.GroupLayout(jxPainelEstrutural);
        jxPainelEstrutural.setLayout(jxPainelEstruturalLayout);
        jxPainelEstruturalLayout.setHorizontalGroup(
            jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxPainelEstruturalLayout.createSequentialGroup()
                .addGroup(jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jxPainelEstruturalLayout.createSequentialGroup()
                        .addComponent(jlabelImagemTopo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 184, Short.MAX_VALUE)
                        .addComponent(jxlabelBotaoFechar, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jxPainelEstruturalLayout.createSequentialGroup()
                        .addContainerGap(150, Short.MAX_VALUE)
                        .addGroup(jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jlabelNome, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                            .addComponent(jlabelSenha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jlabelConfirmarSenha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jpasswordfieldSenha)
                            .addComponent(jtextfieldNome, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
                            .addComponent(jpasswordfieldConfirmarSenha)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jxPainelEstruturalLayout.createSequentialGroup()
                        .addContainerGap(297, Short.MAX_VALUE)
                        .addComponent(jxlabelBotaoContinuar, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jxPainelEstruturalLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jlabelRegistrar)))
                .addContainerGap())
        );
        jxPainelEstruturalLayout.setVerticalGroup(
            jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxPainelEstruturalLayout.createSequentialGroup()
                .addGroup(jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jxPainelEstruturalLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jlabelImagemTopo))
                    .addComponent(jxlabelBotaoFechar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jlabelRegistrar)
                .addGap(18, 18, 18)
                .addGroup(jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtextfieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlabelNome, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jpasswordfieldSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlabelSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jpasswordfieldConfirmarSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlabelConfirmarSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addComponent(jxlabelBotaoContinuar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(48, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jxPainelInvisivelLayout = new javax.swing.GroupLayout(jxPainelInvisivel);
        jxPainelInvisivel.setLayout(jxPainelInvisivelLayout);
        jxPainelInvisivelLayout.setHorizontalGroup(
            jxPainelInvisivelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jxPainelEstrutural, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jxPainelInvisivelLayout.setVerticalGroup(
            jxPainelInvisivelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jxPainelEstrutural, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jxPainelInvisivel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jxPainelInvisivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
    alpha = 0.f;
    final Timer tempo = new Timer();              //Temporizador
    TimerTask tarefa = new TimerTask() {     //Objeto tarefa a ser realizada

        public void run() {                 //Metodo run que implementa a tarefa
            if ((alpha + 0.05) <= 1.0f) {
                alpha = alpha + 0.05f;
            } else {
                tempo.cancel();
            }
            setAlpha();
        }
    };
    tempo.schedule(tarefa, 50, 5);
}//GEN-LAST:event_formComponentShown

private void jxlabelBotaoContinuarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoContinuarMouseClicked
    this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    String nome = jtextfieldNome.getText();
    char[] senhaTemp = jpasswordfieldSenha.getPassword();
    char[] confirmacaoTemp = jpasswordfieldConfirmarSenha.getPassword();
    String senha = String.valueOf(senhaTemp);
    String confirmacao = String.valueOf(confirmacaoTemp);
    if (confirmacao.compareTo(senha) == 0) {
        Usuario usuarioTemporario = new Usuario(nome);
        dadosContas = new DadosContas(ConstantesDeCaminho.arquivoDeContas);
        contaTemporaria = new Conta(usuarioTemporario);
        contas = dadosContas.getContaByUsuario(usuarioTemporario);
        if (contas.isEmpty()) {
            firePropertyChange("Continuar", null, contaTemporaria);
        } else {
            JOptionPane.showMessageDialog(null, "Já existe um usuário com esse nome!");
            jtextfieldNome.setText("");
            jpasswordfieldSenha.setText("");
            jpasswordfieldConfirmarSenha.setText("");
            jtextfieldNome.requestFocus();
        }
        dadosContas.closeDatabase();
    } else {
        JOptionPane.showMessageDialog(null, "Confirmação incorreta!");
    }
    this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_jxlabelBotaoContinuarMouseClicked

private void jxlabelBotaoContinuarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoContinuarMouseEntered
    Eventos.mouseEntraBotao(evt);
}//GEN-LAST:event_jxlabelBotaoContinuarMouseEntered

private void jxlabelBotaoContinuarMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoContinuarMouseExited
    Eventos.mouseSaiBotao(evt);
}//GEN-LAST:event_jxlabelBotaoContinuarMouseExited

private void jxlabelBotaoFecharMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoFecharMouseClicked
    firePropertyChange("Fechar", null, null);
}//GEN-LAST:event_jxlabelBotaoFecharMouseClicked

private void jxlabelBotaoFecharMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoFecharMouseEntered
    Eventos.mouseEntraFechar(evt);
}//GEN-LAST:event_jxlabelBotaoFecharMouseEntered

private void jxlabelBotaoFecharMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoFecharMouseExited
    Eventos.mouseSaiFechar(evt);
}//GEN-LAST:event_jxlabelBotaoFecharMouseExited

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jlabelConfirmarSenha;
    private javax.swing.JLabel jlabelImagemTopo;
    private javax.swing.JLabel jlabelNome;
    private javax.swing.JLabel jlabelRegistrar;
    private javax.swing.JLabel jlabelSenha;
    private javax.swing.JPasswordField jpasswordfieldConfirmarSenha;
    private javax.swing.JPasswordField jpasswordfieldSenha;
    private javax.swing.JTextField jtextfieldNome;
    private org.jdesktop.swingx.JXPanel jxPainelEstrutural;
    private org.jdesktop.swingx.JXPanel jxPainelInvisivel;
    private org.jdesktop.swingx.JXLabel jxlabelBotaoContinuar;
    private org.jdesktop.swingx.JXLabel jxlabelBotaoFechar;
    // End of variables declaration//GEN-END:variables
}
